﻿using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.codec;

namespace B_Modules.iTextSharp
{
    class TestITextSharp
    {

        private static readonly string BasePath = AppDomain.CurrentDomain.BaseDirectory;

        private readonly string _resource1 = BasePath +
                                                @"\iTextSharp\Src\PDF01.pdf";

        private readonly string _resource2 = BasePath +
                                    @"\iTextSharp\Src\Src2.tif";

        private readonly string _output1 = BasePath +
                                    @"\iTextSharp\Src\PDF02.pdf";


        public void PDFCopyPages()
        {
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(
            document, File.Create(_output1));
            document.Open();
            PdfPTable table = new PdfPTable(1);
            PdfReader reader = new PdfReader(_resource1);
            int n = reader.NumberOfPages;
            PdfImportedPage page;
            //for (int i = 1; i <= n; i++)
            //{
                

            //    page = writer.GetImportedPage(reader, i);
            //    table.AddCell(Image.GetInstance(page));
    
            //}
            page = writer.GetImportedPage(reader, 1);
            var vIamge = Image.GetInstance(page);
            var mImage = Image.GetInstance(_resource2);
            //mImage.MakeMask();
            //vIamge.ImageMask = mImage;

            mImage.SetAbsolutePosition(0,0);
            vIamge.SetAbsolutePosition(0,0);
            mImage.ScaleToFit(vIamge.Width+50,vIamge.Height+50);
            //mImage.Alignment = Image.UNDERLYING;

            //document.SetMargins(1,1,1,1);
            document.Add(mImage);
            document.Add(vIamge);

            
            document.Close();
            //writer.Close();
        }


        public void Test1()
        {
            //PdfReader pdfReader = new PdfReader(RESOURCE1);

            var outputStream =
                File.Create(_output1);

            Document document = new Document();

            var writer =
            PdfWriter.GetInstance(document, outputStream);
            //writer.Open();
            document.Open();

            var table1 = new PdfPTable(1);

            var inputImageStream =
                File.OpenRead(_resource2);

            var inputImage = Image.GetInstance(inputImageStream);

            inputImage.SetAbsolutePosition(0,0);
            inputImage.Alignment = Image.UNDERLYING;

            document.Add(table1);
            document.Add(inputImage);

            //writer.Flush();
            //writer.Close();
            document.Close();




            return;











            PdfCopy _docWriter = new PdfCopy(document, outputStream);

            //var asd = document.Open();

            PdfPTable table = new PdfPTable(2);
            PdfReader reader = new PdfReader(_resource1);
            int n = reader.NumberOfPages;
            PdfImportedPage page;

            for (int i = 1; i <= n; i++)
            {

               
                page = writer.GetImportedPage(reader, i);
                //_docWriter.AddPage(page);
                //document.Add(page);
                //table.AddCell(Image.GetInstance(page));
            }
            //document.Add(table);
            
            //writer.Close();


            var _img =
            PngImage.GetImage(_resource2);
            //_img.
            //document.Add(_img);
            _docWriter.Add(_img);
            _docWriter.Close();

            //document.Close();




            //var list =
            //    this.GetTif(RESOURCE2);

            //foreach (var image in list)
            //{
            //    document.Add(image);
            //}


            document.Close();



            //TiffImage sdf = new TiffImage();
            //TiffImage.
            //sdf.

            //Image _
        }



        public IList<Image> GetTif(String path)
        {
            IList<Image> list = new List<Image>();
            var ra = new RandomAccessFileOrArray(_resource2);
            int n = TiffImage.GetNumberOfPages(ra);
            Image img;
            for (int i = 1; i <= n; i++) {
                img = TiffImage.GetTiffImage(ra, i);
                //img.ScaleToFit(523, 350);
                list.Add(img);
            }
            return list;
        }
    }
}
